#ifndef HASHROWSYNCHRONIZEDIMPL_H_JM_20120910
#define HASHROWSYNCHRONIZEDIMPL_H_JM_20120910

//#include <boost/thread/mutex.hpp>
//#include <tbb/mutex.h>
#include "upp_mthr.hpp"

#include "HashRow.h"


namespace jm4bbx {

	template<class T>
	class HashRowSynchronizedImpl : public HashRow<T>{
	public:

		HashRowSynchronizedImpl(HashRow<T>* hashRow) : hashRow_(hashRow) { }

		int add(const ValueType& value) {
			scopeLockType lock(mutex_);
			return hashRow_->add(value);
		}

		ExtendedValueType getExtendedValue(const size_t& index) const {
			scopeLockType lock(mutex_);
			return hashRow_->getExtendedValue(index);
		}

		bool isEmpty() const {
			scopeLockType lock(mutex_);
			return hashRow_->isEmpty();
		}

		void clear() {
			scopeLockType lock(mutex_);
			hashRow_->clear();
		}

		size_t distinctSize() const {
			scopeLockType lock(mutex_);
			return hashRow_->distinctSize();
		}

		std::pair<int, int> foreach(ForechValueFunType functor, const std::pair<int, int>& iterationContext) override {
			scopeLockType lock(mutex_);
			return hashRow_->foreach(functor, iterationContext);
		}

		int foreachExtended(ForechExtendedValueFunType functor, int iterationContext) override {
			scopeLockType lock(mutex_);
			return hashRow_->foreachExtended(functor, iterationContext);
		}


		virtual HashRow<T>* getLockFreeInstance() {
			return hashRow_->getLockFreeInstance();
		};
		
		virtual HashRow<T>* getLockableInstance() {
			return this;
		};

	private:
		typedef ulibpp::mutex mutexType;
		typedef ulibpp::scope_lock<mutexType> scopeLockType;
		HashRow<T>* hashRow_;
		mutable mutexType mutex_;
		
	};
	
} // namespace jm4bbx

#endif
